'use strict';
var load = 1;
var page = 1;
var status = 1;
$(function(){
    paged(page,status)
    $(".order_nav .ordernav_item").on("click",function(){
        $(".order_list").html("");
        page=1;
        load = 1;
        $(this).addClass("on").siblings(".ordernav_item").removeClass("on")
        status = $(this).index() + 1;
        paged(page,status)
	})
    function paged(page,status){
        // 租用记录
        $.ajax({
            type:"post",
            url:url+'/trade/myorder.html?token='+token,
            async:true,
            data:{'token':token,"page":page,"status":status},
            dataType:'json',
            success:function(result){
                // console.log(result)
                if(result.status === "0001"){
                    var data = result.data;
                    var html = "";
                    if(data.length > 0){
                        for(var i = 0;i < data.length;i++){
                            var o = data[i];
                            var rmb = (o.rmb).toFixed(2);
                            html += `<li class="order_item">
                                <p class="order_title">${o.type == 1?"买入":"卖出"}SDC<span class="order_titlesp on">${o.status}</span></p>
                                <p class="order_title">${o.type == 1?"买入数量":"卖出数量"}<span class="order_titlesp">${o.number} SDC</span></p>
                                <p class="order_title">${o.type == 1?"买入单价":"买出单价"}<span class="order_titlesp">${o.sysprice} USD</span></p>
                                <p class="order_title">${o.type == 1?"买入总价":"买出总价"}<span class="order_titlesp">${o.amount} USD</span></p>
                                <p class="order_title">合计人民币<span class="order_titlesp">${rmb} CNY </span></p>`
                                if(o.type != 1){
                                    html += `<p class="order_title">手续费<span class="order_titlesp on2">${o.terrace_fee}</span></p>`
                                }
                                html += `<p class="order_title">${o.type == 1?"实收数量":"实付数量"}<span class="order_titlesp">${o.total} SDC</span></p>
                                <p class="order_title">创建时间<span class="order_titlesp">${o.created_at}</span></p>`

                            if(o.status != "已完成" && o.status != "已取消"){
                                var status = o.status == "进行中" ? 1 : 2;
                                html += `<a href="order_details1.html?id=${o.id}&status=${status}" class="order_link">查看详情</a> `
                            }
                            html += `</li>` 
                        }
                        if(data.length < 5){
                            load = 0;
                            html += "<li class='noMoreMsg' style='text-align:center;display:block;margin-bottom:.2rem;'>暂无数据</li>"; 
                        }
                        $(".order_list").append(html);
                    }else{
                        load = 0;
						html += "<li class='noMoreMsg' style='text-align:center;display:block;margin-bottom:.2rem;'>暂无数据</li>";
                        $(".order_list").append(html);
                        return false;
					}
                }else{
                    isErr(result)
                }
            },
            error:function(){
                layer.msg("响应失败");
            }
        })
    }
    
   // 下拉加载更多
   $(window).scroll(function () {
    if ($(document).scrollTop() >= $(document).height() - $(window).height() - 1) {
        if (load == 1) {
            page++;
            paged(page,status)
        }
    }
});
})